package com.zhiyume.service;

import com.zhiyume.dao.api.TDramaPersonMapper;
import com.zhiyume.dao.api.TPersonMapper;
import com.zhiyume.dao.api.TPersonSelfMapper;
import com.zhiyume.dao.entity.TDramaPerson;
import com.zhiyume.dao.entity.TDramaPersonCriteria;
import com.zhiyume.dao.entity.TPerson;
import com.zhiyume.dao.entity.TPersonCriteria;
import com.zhiyume.dao.entity.TPersonWithBLOBs;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.util.TypeConstants;
import com.zhiyume.vo.PersonReqVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TPersonService {
    @Autowired
    private TPersonMapper tPersonMapper;
    @Autowired
    private TPersonSelfMapper tPersonSelfMapper;
    @Autowired
    private TDramaPersonMapper tDramaPersonMapper;

    /**
     * 根据Id查询人员信息
     * @return
     */
    public TPersonWithBLOBs findPersonById(long id){
        TPersonCriteria example = new TPersonCriteria();
        TPersonCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TPersonWithBLOBs> personList = tPersonMapper.selectByExampleWithBLOBs(example);
        if(null != personList && personList.size() > 0) {
            return personList.get(0);
        }

        return new TPersonWithBLOBs();
    }

    /**
     * 根据Id查询人员信息
     * @return
     */
    public List<TPerson> findActorById(long id){
        TPersonCriteria example = new TPersonCriteria();
        TPersonCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TPerson> personList = tPersonMapper.selectByExample(example);
        if(null != personList && personList.size() > 0) {
            return personList;
        }

        return new ArrayList<>();
    }


    /**
     * 根据Id查询人员信息
     * @return
     */
    public TPersonWithBLOBs findPersonWithBLOBsById(long id){
        TPersonCriteria example = new TPersonCriteria();
        TPersonCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TPersonWithBLOBs> personList = tPersonMapper.selectByExampleWithBLOBs(example);
        if(null != personList && personList.size() > 0) {
            return personList.get(0);
        }

        return new TPersonWithBLOBs();
    }



    /**
     * 根据name查询人员信息
     * @return
     */
    public String findPersonByName(String name){
        TPersonCriteria example = new TPersonCriteria();
        TPersonCriteria.Criteria c = example.createCriteria();
        c.andNameLike("%"+name+"%");
        List<TPerson> personList = tPersonMapper.selectByExample(example);
        if(null != personList && personList.size() > 0) {
            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg(),personList);
        }

        return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_NO_DATA.getErrType(),ConstantEnum.CONSTANT_NO_DATA.getErrMsg());
    }

    /**
     * 添加人员信息
     * @param tPerson
     * @return
     */
    public String addPerson(TPersonWithBLOBs tPerson) {

        long id = GenerateKeyUtil.generationPK();
        tPerson.setId(id);
        tPerson.setCreateTime(DateUtil.getNowFormatYYYYMDD());
        tPerson.setRepWorks(TypeConstants.PersonConstant.DefaultRepWorks);
        if(tPersonMapper.insert(tPerson)>0)
            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
        return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_ADD_DATE_ERROR.getErrType(),ConstantEnum.CONSTANT_ADD_DATE_ERROR.getErrMsg());
    }
    /**
     * 修改人员信息
     * @param tPerson
     * @return
     */
    public String updatePerson(TPersonWithBLOBs tPerson) {

        TPersonCriteria example = new TPersonCriteria();
        TPersonCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tPerson.getId());
        tPersonMapper.updateByExample(tPerson,example);
//        if(tPersonMapper.updateByExample(tPerson,example)>0)
//            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
//        return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_UPDATE_DATE_ERROR.getErrType(),ConstantEnum.CONSTANT_UPDATE_DATE_ERROR.getErrMsg());
        return null;
    }
    /**
     * 逻辑删除人员
     * @param id
     * @return
     */
    public String deletePerson(long id) {
        if(tPersonSelfMapper.delectPersonById(id)>0)
            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
        return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_UPDATE_DATE_ERROR.getErrType(),ConstantEnum.CONSTANT_UPDATE_DATE_ERROR.getErrMsg());
    }


    public List<TPersonWithBLOBs> queryPersonList(PersonReqVO dto) {
        TPersonCriteria tPersonCriteria = new TPersonCriteria();
        TPersonCriteria.Criteria criteria = tPersonCriteria.createCriteria();
        tPersonCriteria.setLimitStart(dto.getPage()*dto.getPageSize());
        tPersonCriteria.setLimitEnd(dto.getPageSize()*dto.getPageSize()+dto.getPageSize());

        createCriteria(dto, criteria);
        return tPersonMapper.selectByExampleWithBLOBs(tPersonCriteria);
    }

    public List<TPersonWithBLOBs> queryActorList(PersonReqVO dto) {
        TPersonCriteria tPersonCriteria = new TPersonCriteria();
        TPersonCriteria.Criteria criteria = tPersonCriteria.createCriteria();

        criteria.andCareerEqualTo(TypeConstants.PersonConstant.PERSON_ACTOR);
        criteria.andStatusEqualTo(0);
        tPersonCriteria.setLimitStart(dto.getPage()*dto.getPageSize());
        tPersonCriteria.setLimitEnd(dto.getPageSize());
        createCriteria(dto, criteria);
        return tPersonMapper.selectByExampleWithBLOBs(tPersonCriteria);
    }

    public List<TPersonWithBLOBs> searchActorList(PersonReqVO dto){
        Map map = new HashMap();
        map.put("limitStart",dto.getPage()*dto.getPageSize());
        map.put("limitEnd",dto.getPageSize());
        map.put("name",dto.getName());
        map.put("status","0");
        map.put("career",TypeConstants.PersonConstant.PERSON_ACTOR);
        return tPersonSelfMapper.selectListByMap(map);
    }

    private void createCriteria(PersonReqVO dto, TPersonCriteria.Criteria criteria) {

        if(StringUtil.isNotBlank(dto.getName())){
            criteria.andNameLike("%"+dto.getName()+"%");
        }
        if(StringUtil.isNotBlank(dto.getContact())){
            criteria.andContactLike("%"+dto.getContact()+"%");
        }
        if(-1!=dto.getGender()){
            criteria.andGenderEqualTo(dto.getGender());
        }
        if(-1!=dto.getHeight()){
            criteria.andHeightEqualTo(dto.getHeight());
        }
        if(null!=dto.getBirthBegin()){
            criteria.andBirthGreaterThanOrEqualTo(DateUtil.shortDate(dto.getBirthBegin()));
        }
        if(null!=dto.getBirthEnd()){
            criteria.andBirthLessThanOrEqualTo(DateUtil.shortDate(dto.getBirthEnd()));
        }
    }

    public int countPerson(PersonReqVO dto) {
        TPersonCriteria tPersonCriteria = new TPersonCriteria();
        TPersonCriteria.Criteria criteria = tPersonCriteria.createCriteria();

        createCriteria(dto, criteria);
        return tPersonMapper.countByExample(tPersonCriteria);
    }

    public int countActor(PersonReqVO dto) {
        TPersonCriteria tPersonCriteria = new TPersonCriteria();
        TPersonCriteria.Criteria criteria = tPersonCriteria.createCriteria();

        criteria.andCareerEqualTo(TypeConstants.PersonConstant.PERSON_ACTOR);
        criteria.andStatusEqualTo(0);
        createCriteria(dto, criteria);
        return tPersonMapper.countByExample(tPersonCriteria);
    }

    public int cntActor(PersonReqVO dto){
        Map map = new HashMap();
        map.put("limitStart",dto.getPage()*dto.getPageSize());
        map.put("limitEnd",dto.getPageSize());
        map.put("name",dto.getName());
        map.put("status","0");
        map.put("career",TypeConstants.PersonConstant.PERSON_ACTOR);
        return tPersonSelfMapper.selectCntByMap(map);
    }

    public List getRepWorks(PersonReqVO dto){
        List repWorks = new ArrayList();
        TDramaPersonCriteria t = new TDramaPersonCriteria();
        TDramaPersonCriteria.Criteria c = t.createCriteria();
        if(dto.getId()!=null){
            c.andPIdEqualTo(dto.getId());
        }
        List<TDramaPerson> list = tDramaPersonMapper.selectByExample(t);
        if(CollectionUtils.isNotEmpty(list)){
            for(TDramaPerson tp:list){
                Map temp = new HashedMap();
                temp.put("dId",tp.getDramaId());
                temp.put("dName",tp.getDramaName());
                repWorks.add(temp);
            }
        }
        return repWorks;
    }
    public List<TDramaPerson> getDramaPerson (Long id){
        TDramaPersonCriteria example = new TDramaPersonCriteria();
        TDramaPersonCriteria.Criteria c = example.createCriteria();
        c.andDramaIdEqualTo(id);
        List<TDramaPerson> list = tDramaPersonMapper.selectByExample(example);
        return list;
    }
}
